package com.xingdata.jjxc.activity;

import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.SystemInfo;

import jar.MessageManager;
import jar.model.RequestEntity;
import jar.tools.InstructionSet;

import static com.xingdata.jjxc.R.id.cof_navipathtype;
import static com.xingdata.jjxc.R.id.cof_navivoice;

/**
 * Created by ysc on 10/12/2016.
 */

public class NaviSetActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{
    private String TAG = "NaviSetActivity";

    private int[] groupIDs = {cof_navipathtype, cof_navivoice};


    @Override
    protected int getContentViewId() {
        return R.layout.activity_naviset;
    }

    @Override
    protected String getTitleText() {
        return "导航设置";
    }

    @Override
    protected void initViews() {
        Typeface font = Typeface.createFromAsset(getAssets(), JUtils.TypefacePath);

        ((RadioButton) findViewById(R.id.rb_1)).setTypeface(font);
        ((RadioButton) findViewById(R.id.rb_2)).setTypeface(font);
        ((RadioButton) findViewById(R.id.rb_3)).setTypeface(font);

        ((TextView) findViewById(R.id.tv_navi_voice)).setTypeface(font);

        ImageView titile_back = (ImageView) findViewById(R.id.title_back);
        titile_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initSharedValue(groupIDs);
        initRadioGroup(groupIDs);
    }

    private void initSharedValue(int[] ids) {
        for (int id : ids) {
            View view = findViewById(id);
            if (view instanceof RadioGroup) {
                RadioGroup group = (RadioGroup) view;
                String groupTag = group.getTag().toString();
                String sharedValue = SharedPreTools.getString(this, groupTag, "");
                RadioButton radioButton = null;
                if (sharedValue != null && sharedValue.length() != 0) {
                    radioButton = (RadioButton) group.findViewWithTag(sharedValue);
                } else {
                    for (int i = 0;i<group.getChildCount();++i){
                        View c = group.getChildAt(i);
                        if(null != c && c.getId() == id && RadioButton.class.isInstance(c)){
                            radioButton = (RadioButton)c;
                        }
                    }


                }
                if (radioButton != null) {
                    radioButton.setChecked(true);
                }

            }

        }

    }

    private void initRadioGroup(int[] ids) {
        for (int i = 0; i < ids.length; i++) {
            RadioGroup radioGroup = (RadioGroup) findViewById(ids[i]);
            radioGroup.setOnCheckedChangeListener(this);
            // ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        }

    }

    boolean isFirst = true;
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (isFirst) {
            isFirst = false;
        } else {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            if (group.getTag() != null && radioButton.getTag() != null) {
                setSharedValue(group.getTag().toString(), radioButton.getTag().toString());
                sendToHUD();
            }
        }
    }

    private void setSharedValue(String key, String value) {
        Log.i(TAG, "SharedPreferences >" + "value: " + key + "value: " + value);
        SharedPreTools.putString(this, key, value);
    }

    private void sendToHUD() {
        JSONObject jsonObject = new JSONObject();
        String key = null;
        for (int id : groupIDs) {
            RadioGroup group = (RadioGroup) findViewById(id);
            int checkID = group.getCheckedRadioButtonId();
            if (checkID == -1) {
                return;
            }
            String strTag = group.findViewById(checkID).getTag().toString();
            switch (id) {
                // 导航路线规划：时间最短-0、躲避拥堵-4、避开收费-1
                case R.id.cof_navipathtype:
                    key = Common.COF_NAVIPATHTYPE;
                    break;
                // 导航语音播报
                case R.id.cof_navivoice:
                    key = Common.COF_NAVIVOICE;
                    break;
                default:
                    break;
            }
            jsonObject.put(key, strTag);

        }
        Log.i(TAG,"发送至HUD设置:"+jsonObject.toJSONString());
        MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), new RequestEntity(jsonObject.toJSONString(),
                InstructionSet.INSTRUCTION_AUTH_OPTIONS_TO_HUD + ""));
    }
}
